package com.hnucm.chapter3_2;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.yanzhenjie.recyclerview.OnItemMenuClickListener;
import com.yanzhenjie.recyclerview.SwipeMenu;
import com.yanzhenjie.recyclerview.SwipeMenuBridge;
import com.yanzhenjie.recyclerview.SwipeMenuCreator;
import com.yanzhenjie.recyclerview.SwipeMenuItem;
import com.yanzhenjie.recyclerview.SwipeRecyclerView;
import com.yanzhenjie.recyclerview.widget.DefaultItemDecoration;

import java.util.ArrayList;
import java.util.List;


public class MainActivity extends AppCompatActivity {



    List list=new ArrayList();
    int width = 100;
    int height = ViewGroup.LayoutParams.MATCH_PARENT;
    private MyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(MainActivity.this,SwipeActivity.class);
                startActivity(intent);
            }
        });

        for(int i=0;i<10;++i){
            list.add(i);
        }

        SwipeRecyclerView swipeRecyclerView = findViewById(R.id.recycler_view);

        // 设置菜单创建器。
        swipeRecyclerView.setSwipeMenuCreator(new SwipeMenuCreator() {
              @Override
              public void onCreateMenu(SwipeMenu leftMenu, SwipeMenu rightMenu, int position) {
                  SwipeMenuItem upItem = new SwipeMenuItem(MainActivity.this)
                          .setBackgroundColor(Color.BLACK)
                          .setText("置顶")
                          .setTextColor(Color.WHITE)
                          .setWidth(width)
                          .setHeight(height);
                  rightMenu.addMenuItem(upItem); //

                  SwipeMenuItem deleteItem = new SwipeMenuItem(MainActivity.this)
                          .setBackgroundColor(Color.BLUE)
                          .setText("删除")
                          .setTextColor(Color.BLACK)
                          .setWidth(width)
                          .setHeight(height);
                  rightMenu.addMenuItem(deleteItem);// 添加一个按钮到右侧侧菜单。.
                  // 上面的菜单哪边不要菜单就不要添加。
              }
          });

        swipeRecyclerView.setOnItemMenuClickListener(new OnItemMenuClickListener() {
            @Override
            public void onItemClick(SwipeMenuBridge menuBridge, int adapterPosition) {
                // 任何操作必须先关闭菜单，否则可能出现Item菜单打开状态错乱。
                menuBridge.closeMenu();
                // 左侧还是右侧菜单：
                int direction = menuBridge.getDirection();
                // 菜单在Item中的Position：
                int menuPosition = menuBridge.getPosition();
                if (direction == SwipeRecyclerView.LEFT_DIRECTION) {
                    switch (menuPosition) {
                        case 1:
                            Toast.makeText(MainActivity.this, "左侧第一个", Toast.LENGTH_SHORT).show();
                            break;
                        case 2:
                            Toast.makeText(MainActivity.this, "左侧第2个", Toast.LENGTH_SHORT).show();
                            break;
                        default:
                            Toast.makeText(MainActivity.this, "默认", Toast.LENGTH_SHORT).show();
                            break;
                    }
                } else if (direction == SwipeRecyclerView.RIGHT_DIRECTION) {
                    switch (menuPosition) {
                        case 0: {
                            Toast.makeText(MainActivity.this, "右侧第一个", Toast.LENGTH_SHORT).show();
                            list.add(0,list.get(adapterPosition));
                            list.remove(adapterPosition+1);
                            adapter.notifyDataSetChanged();
                        }
                        break;
                        case 1: {
                            Toast.makeText(MainActivity.this, "右侧第2个", Toast.LENGTH_SHORT).show();
                            list.remove(adapterPosition);
                            adapter.notifyDataSetChanged();
                        }break;
                        default:
                            Toast.makeText(MainActivity.this, "默认", Toast.LENGTH_SHORT).show();
                            break;
                    }
                }
            }
        });
        swipeRecyclerView.addItemDecoration(new DefaultItemDecoration(Color.BLACK));//分割线
        adapter=new MyAdapter();//创建适配器
        swipeRecyclerView.setAdapter(adapter);//设置适配器
        swipeRecyclerView.setLayoutManager(new LinearLayoutManager(this));//设置布局
    }
    public class MyAdapter extends SwipeRecyclerView.Adapter<MyViewHolder>{
        @NonNull
        @Override
        public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view=LayoutInflater.from(MainActivity.this).inflate(R.layout.item_swiperecyclerview,parent,false);
            MyViewHolder myViewHolder=new MyViewHolder(view);
            return myViewHolder;
        }
        @Override
        public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
            holder.textView.setText("文本"+list.get(position));
        }
        @Override
        public int getItemCount() {
            return list.size();
        }
    }
    public class MyViewHolder extends SwipeRecyclerView.ViewHolder{

        TextView textView;
        public MyViewHolder(@NonNull View itemView) {
            super(itemView);
            textView=itemView.findViewById(R.id.textView);
        }
    }
}